package com.lgx.mall.business.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mall.business.dao.IConsumerInfoDao;
import com.lgx.mall.business.entity.ConsumerInfo;
import com.lgx.right.entity.User;

/**
 * 
 * @author acer
 *
 */
@Component("consumerInfoDao")
public class ConsumerInfoDao extends GeneralDao implements IConsumerInfoDao {

	protected Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}
	
	@Transactional
	public ConsumerInfo createConsumerInfo(ConsumerInfo consumerInfo)throws DataException{
		this.getHibernateTemplate().save(consumerInfo);
		log.info("[创建会员消费]:"+consumerInfo.getId());
		return consumerInfo;
	}

	public ConsumerInfo findConsumerInfoById(long id) {
		return this.getHibernateTemplate().get(ConsumerInfo.class, id);
	}
	
	public Pagination findConsumerInfoPagination(Object[] params, int pageSize, int pageNum){
		String sql = "from ConsumerInfo c ";
		
		sql += " where 1=1";
		
		if(params[0] != null){
			if(params[1] != null){
				sql += " and unix_timestamp(c.consumerTime) between unix_timestamp('"+params[0]+"') and unix_timestamp('"+params[1]+"')";
			}else{
				sql += " and unix_timestamp(c.consumerTime) > unix_timestamp('"+params[0]+"')";
			}
		}else{
			if(params[1] != null){
				sql += " and unix_timestamp(c.consumerTime) < unix_timestamp('"+params[1]+"')";
			}
		}
		
		if(params[6] != null){
			if(params[7] != null){
				sql += " and c.amount between "+params[6]+" and "+params[7];
			}else{
				sql += " and c.amount > "+params[6];
			}
		}else{
			if(params[7] != null){
				sql += " and c.amount < "+params[7];
			}
		}
		
		sql += " and c.userMobile like '%"+params[2]+"%'";
		sql += " and c.userCardId like '%"+params[3]+"%'";
		sql += " and c.orgName like '%"+params[5]+"%'";
		
		if(params[4] != null){
			sql += " and c.type = "+params[4];
		}
		
		sql += " order by c.id asc";
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}
	
	@SuppressWarnings("unchecked")
	public List<User> findUserByCardIdOrMobile(String userCardIdOrMobile){
		String sql = "from User u where u.isDelete = '0' ";
		sql += " and (u.mobile = '"+userCardIdOrMobile+"' or u.card.id = '"+userCardIdOrMobile+"')" ;
		return this.getHibernateTemplate().find(sql);
	}

	/**
	 * 根据用户id检索消费记录
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ConsumerInfo> findConsumerInfos(Long userId) {
		String sql = "from ConsumerInfo c where c.isDelete = '0' and  c.userId = "+userId;
		return this.getHibernateTemplate().find(sql);
	}

	/**
	 * 更改信息显示状态
	 */
	@Override
	public void updateRechargeInfo(ConsumerInfo consumerInfo)
			throws DataException {
		log.info("[修改]:"+consumerInfo.getId());
		this.getHibernateTemplate().update(consumerInfo);
	}

}
